Imports DAL
Imports MyClasses
Imports System.Configuration
Public Class frmItemMiscCd
    Public blnItemMaster As Integer

    Private Sub frmItemMiscCd_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Call BCommon.SetFormStandards(Me)
        Call DisplayData()
        txtID.Focus()
    End Sub

    Private Sub DisplayData()
        Try
            Call ClearData()
            Call ViewCodeIdDesc()
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "DisplayData")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub ClearData()
        txtID.Text = ""
        txtDescp.Text = ""
        txtID.Tag = ""
        txtDescp.Tag = ""
    End Sub

    Private Sub ViewCodeIdDesc()
        Try
            cmbViewDesc.DataSource = BMiscCode.GetCodeIdDesc("Item Form")
            cmbViewDesc.Splits(0).DisplayColumns(0).Width = cmbViewDesc.Width * 25 / 100
            cmbViewDesc.Splits(0).DisplayColumns(1).Width = cmbViewDesc.Width * 70 / 100
            cmbViewDesc.Columns(0).Caption = "CodeID"
            cmbViewDesc.Columns(1).Caption = "Code Description"
            cmbViewDesc.DisplayMember = "Code Description"
            cmbViewDesc.Tag = cmbViewDesc.Text
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ViewCodeIdDesc")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub cmbViewDesc_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbViewDesc.Leave
        cmbViewDesc.SelectedIndex = -1
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If ValidateData() Then
            If SaveData() Then Me.Close()
        End If
    End Sub

    Private Function ValidateData() As Boolean
        Dim dtCodeId As New DataTable
        Dim count As Integer
        Try

            If Trim(txtID.Text) = "" Then
                BCommon.ShowMsgError(10004)
                txtID.Focus()
                Return False
            End If
            If btnSave.Visible = True Then
                dtCodeId = BMiscCode.GetCodeIdDesc("Item Form")
                If dtCodeId.Rows.Count > 0 Then
                    For count = 0 To dtCodeId.Rows.Count - 1
                        If Trim(txtID.Text) = dtCodeId.Rows(count).Item("CodeId") Then
                            BCommon.ShowMsgError(10006)
                            txtID.Text = ""
                            txtID.Focus()
                            Return False
                        End If
                    Next
                End If
            End If
            If Trim(txtDescp.Text) = "" Then
                BCommon.ShowMsgError(10005)
                txtDescp.Focus()
                Return False
            End If
            Return True

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ValidateData")
            Return False
        End Try
    End Function

    Private Function SaveData() As Boolean
        Dim MiscCode As New CMiscCd
        Dim blnFlag As Boolean
        Try
            MiscCode = SetData()
            blnFlag = BMiscCode.Save(MiscCode)

            If blnFlag = True Then
                BCommon.ShowMsgError(3)
                blnItemMaster = 1
                Return True
            Else : BCommon.ShowMsgError(4) : Return False
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SaveData")
            Me.Cursor = Cursors.Default
        End Try
    End Function

    Private Function SetData() As CMiscCd
        Dim MiscCode As New CMiscCd
        Try
            MiscCode.Code_Type = "ITEMFORM"
            MiscCode.Code_ID = txtID.Text
            MiscCode.Description = txtDescp.Text
            Return MiscCode
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetData")
            Me.Cursor = Cursors.Default
            Return Nothing
        End Try
    End Function

    Private Sub txtID_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtID.KeyDown
        If e.KeyCode = Keys.Enter Then txtDescp.Focus()
    End Sub

    Private Sub txtDescp_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtDescp.KeyDown
        If e.KeyCode = Keys.Enter Then
            btnSave.Focus()
        Else
            If BCommon.ValidateText(e.KeyCode, "ALFTEXT", False) = False Then
                e.SuppressKeyPress = True
            End If
        End If
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub
End Class